package com.mq.java.test;

import com.mq.common.MqConnectionUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;

/**
 * @author zhd
 * @date 2019/08/17
 **/
public class ProducerDurable {

    public static void main(String[] args) {

        try {
            // 连接到RabbitMQ
            Connection connection = MqConnectionUtils.getConnection();

            Channel channel = connection.createChannel();

            String exchangeName = "durable-exchange";
            channel.exchangeDeclare(exchangeName,
                    BuiltinExchangeType.DIRECT,
                    // 交换器持久化
                    true,
                    false,
                    false,
                    null);

            channel.queueDeclare("durable-queue",
                    // 队列持久化
                    true,
                    false,
                    false,
                    null);

            channel.queueBind("durable-queue",
                    "durable-exchange",
                    "durable-routingKey",
                    null);


            channel.basicPublish(exchangeName,
                    "durable-routingKey",
                    false,
                    false,
                    // 消息持久化
                    // 持久化模式 delivery_mode = 2  持久化消息
                    MessageProperties.MINIMAL_PERSISTENT_BASIC,
                    "1111".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
